
function DataLoad( AfterLoad ) {
	
	window.dataVar={};//建立data容器全局变量
	
	//shop相关局部变量
	var shopInfo = [];
	var shopType = [];
	var shopArea = [];
	
	function division(str, mode) {
		var strs = str.split(" | ");
		if(mode == 1) {
			return strs[0];
		} else if(mode == 2) {
			return strs[1];
		} else if(mode == 3) {
			var strs1 = strs[1].split(" ");
			return strs1[0];
		}
	}

	function InArrayNum(arr, value) {
		for(var i = 0; i < arr.length; i++) {
			if(value === arr[i].region) {
				return i;
			}
		}
		return -1;
	}

	function SetColor() {
		var endR = 255;
		var endG = 73;
		var endB = 13;
		var startR = 255;
		var startG = 255;
		var startB = 255;

		var sR = (endR - startR) / 6;
		var sG = (endG - startG) / 6;
		var sB = (endB - startB) / 6;

		for(var i = 0; i < 6; i++) {
			StarColor.push({
				R: parseInt((sR * i + startR)),
				G: parseInt((sG * i + startG)),
				B: parseInt((sB * i + startB))
			});
		}
	}

	function GetColor(star) {
		star = parseInt(star);
		return StarColor[star];
	}

	function SetAvg() {
		for(var i = 0; i < shopType.numMapLength; i++) {
			var j = shopType.numMap[i];
			shopType[j].star_avg = Math.round(shopType[j].star_avg / shopType[j].amount);
			shopType[j].tast_avg = Math.round(shopType[j].tast_avg / shopType[j].amount);
			shopType[j].environment_avg = Math.round(shopType[j].environment_avg / shopType[j].amount);
			shopType[j].service_avg = Math.round(shopType[j].service_avg / shopType[j].amount);
		}

	}
	
	function setTop(typeName,shop){
		var top_len = shopType[typeName].top3_id.length;
		if(top_len < 3){
			shopType[typeName].top3_id.push(shop.item_id);
		} else {
			for(var i = 0;i < top_len;i++){
				var id = shopType[typeName].top3_id[i];
				var sum1 = shopInfo.GetShopWithID(id).star + shopInfo.GetShopWithID(id).tast + shopInfo.GetShopWithID(id).environment + shopInfo.GetShopWithID(id).service;
				var sum2 = shop.star + shop.tast + shop.environment + shop.service;
				if(sum1 < sum2){
					shopType[typeName].top3_id[i] = shop.item_id;
					break;
				}
			}
		}
	}
	
	//加载店铺信息-------------------------------------------------//
	var StarColor = [];
	shopType.numMapLength = 0;
	shopType.numMap = [];
	shopType.AddType = function(typeName, shopInfo) {
		if(shopType[typeName] == undefined) {
			shopType[typeName] = {
				type: shopInfo.type,
				amount: 1,
				star_avg: shopInfo.star,
				tast_avg: shopInfo.tast,
				environment_avg: shopInfo.environment,
				service_avg: shopInfo.service,
				top3_id:[]
			};
			shopType[typeName].top3_id.push(shopInfo.item_id);
			this.numMap.push(typeName);
			this.numMapLength++;
		} else {
			shopType[typeName].amount++;
			shopType[typeName].star_avg += shopInfo.star;
			shopType[typeName].tast_avg += shopInfo.tast;
			shopType[typeName].environment_avg += shopInfo.environment;
			shopType[typeName].service_avg += shopInfo.service;
			setTop(typeName,shopInfo);
		}
	}
	
	function SetArea(){
		for(var i = 0;i < shopInfo.length;i++){
			var arr_i = InArrayNum(shopArea,shopInfo[i].region);
			if(arr_i == -1){
				shopArea.push({
					region: shopInfo[i].region,
					gross: 1,
					typenum: []
				});
				if(shopArea[shopArea.length-1].typenum[shopInfo[i].type] == undefined){
					shopArea[shopArea.length-1].typenum[shopInfo[i].type] = 1;
				}
			} else {
				shopArea[arr_i].gross++;
				if(shopArea[arr_i].typenum[shopInfo[i].type] == undefined){
					shopArea[arr_i].typenum[shopInfo[i].type] = 1;
				} else {
					shopArea[arr_i].typenum[shopInfo[i].type]++;
				}
			}
		}
	}
	
	shopInfo.IDToIdxMapper = [];
	shopInfo.GetShopWithID = function(id) {
		return shopInfo[shopInfo.IDToIdxMapper[id]];
	}
	
	d3.json("data/item.json", function(error, data) {
		SetColor();
		data.RECORDS.forEach(function(dt, i) {
			shopInfo.push({
				item_id: dt.item_id,
				name: dt.name,
				type: division(dt.item_info, 1), //餐厅类型
				site: division(dt.item_info, 2), //地点
				region: division(dt.item_info, 3), //区域
				star: dt.star, //综合分数
				tast: dt.tast, //口味
				environment: dt.environment, //环境
				service: dt.service, //服务
				color: GetColor(dt.star)
			});

			shopType.AddType(shopInfo[i].type, shopInfo[i]);

			shopInfo.IDToIdxMapper[dt.item_id]=i;
			

		});
		SetAvg();
		SetArea();
		
		dataVar.shopInfo=shopInfo;
		dataVar.shopType=shopType;
		dataVar.shopArea=shopArea;
		MiuTK.isDebug=true;
		MiuTK.log(shopArea);
		LoadUserComment(shopInfo,function(){
			
			//MiuTK.log(window.dataVar);
			AfterLoad();
		});
		
		ShowMap(shopInfo);
	});
}
